#include "ParticleData.h"
#include "Tools/DArray.h"
#include "Game/Game.h"
#include "cocos2d.h"
namespace LTGame
{

	ParticleData::ParticleData(void)
	{	
		/*    flyer.bin -> 1.gameres    */
		FileBin * dis = new FileBin("GameRes/1.gameres");		
		assert(dis!=0);		
		transmitterGroup = readParticleData(TRANSMITTER_GROUP,dis);	
		bulletGroupData = readParticleData(BULLET_GROUP,dis);
		bombGroupData = readParticleData(BOMB_GROUP,dis);
		pathAtom = readPathAtom(dis);
		pathGroupData = readParticleData(PATH_GROUP,dis);

		CC_SAFE_DELETE(dis);
	}
	ParticleData::~ParticleData(void)
	{
		ARRAY3_FREE(transmitterGroup);
		ARRAY3_FREE(bulletGroupData);
		ARRAY3_FREE(bombGroupData);
		ARRAY2_FREE(pathAtom);
		ARRAY3_FREE(pathGroupData);
	}

	short*** ParticleData::readParticleData(ParticleRelatedTypes type, FileBin* dis)
	{
		int i, j, k;
		int len_1, len_2, len_3;
		short*** temp = NULL;

		switch( type )
		{
		case TRANSMITTER_GROUP:
			len_3 = TRANSMITTER_DATA_LEN;
			break;
		case BULLET_GROUP:
			len_3 = BULLET_DATA_LEN;
			break;
		case BOMB_GROUP:
			len_3 = BOMB_DATA_LEN;
			break;
		case PATH_GROUP:
			len_3 = PATHGROUP_DATA_LEN;
			break;
		default:
			return NULL;
		}

		len_1 = dis->readShort();
		assert(len_1 >= 0);

		ARRAY3_NEW1(temp, short, len_1);
		for ( i = 0; i < ARRAY3_LENGHT1(temp); i++ )
		{
			len_2 = dis->readShort();
			ARRAY3_NEW2(temp, i, len_2);

			for ( j = 0; j < ARRAY3_LENGHT2(temp, i); j++ )
			{
				ARRAY_NEW(temp[i][j], short, len_3);
				for ( k = 0; k < ARRAY_LENGTH(temp[i][j]); k++)
				{
					temp[i][j][k] = dis->readShort();
				}

				if ( type == BULLET_GROUP )
				{
					temp[i][j][4] = (short) (temp[i][j][4] <= 0 ? - temp[i][j][4] : 360 - temp[i][j][4]);
				}
			}//END for j
		}//END for i
		return temp;
	}

	signed char** ParticleData::readPathAtom(FileBin* dis)
	{
		int i, j;
		int len_1,len_2;
		signed char** temp = NULL;

		len_1 =  dis->readShort();
		assert(len_1 >= 0);

		ARRAY_NEW(temp, signed char*, len_1);
		for ( i = 0; i < len_1; i++ )
		{
			len_2 =  (dis->readShort()<< 1 );
			ARRAY_NEW(temp[i],signed char, len_2);
			for ( j = 0; j < ARRAY_LENGTH(temp[i]); j++)
			{
				temp[i][j] = dis->readChar();
				if ( temp[i][j] >= 128 )
				{
					temp[i][j] -= 256;
				}
			}
		}

		return temp;
	}
}